<?php

include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include_once(mnminclude.'utils.php');
include(mnminclude.'ts.php');
include(mnminclude.'link.php');
include(mnminclude.'tags.php');
include(mnminclude.'group.php');
include(mnminclude.'search.php');
include(mnminclude.'smartyvariables.php');

force_authentication();

// sidebar
$main_smarty = do_sidebar($main_smarty);

// pagename	
define('pagename', 'group_admin'); 
$main_smarty->assign('pagename', pagename);

if (isset($_REQUEST['grp_id'])) {
	if (isset($_REQUEST['doing'])) {
		$doing = $_REQUEST['doing'];
	}
	else {
		$doing = 'dealapply';
	}
	
	$main_smarty->assign('doing',$doing);

	switch ($doing) {
		case 'editgrpinfo':
			editgrpinfo();
			break;
		case 'sendinvite':
			sendinvite();
			break;
		case 'dealapply':
			dealapply();
			break;
		case 'managemembers':
			managemembers();
			break;
		case 'managerecopeople':
			managerecopeople();
			break;
		case 'managerecothing':
			managerecothing();
			break;
		default:
			sendinvite();
			break;
	}
	
}

if (!isset($_REQUEST['grp_id'])) {
	//create a new group
	$doing = "newgroup";
	$main_smarty->assign("doing",$doing);
	groupnew();
}

function groupnew() {
	global $db, $dblang, $current_user, $main_smarty, $the_template;
	//create a new group
	// show the template
	if (!isset($_POST['grp_name'])) {
		//first time show tpl
		$main_smarty->assign('grp_privacy','open');
		$main_smarty->assign('tpl_center', $the_template . '/groupnew');
		$main_smarty->display($the_template . '/pligg.tpl');
		die();
	}
	else {
		// check the data and create new grp
		$data_wrong = false;
		$tags = $_POST['tags'];
		$grp_name = $_POST['grp_name'];
		$grp_privacy = $_POST['grp_privacy'];
		$grp_desc = $_POST['grp_desc'];
		if (trim($_POST['grp_name']) == '') {
			$data_wrong = true;
			$namecheck = '请输入小组名字';
		}
		if (trim($_POST['grp_desc']) == '') {
			$data_wrong = true;
			$grp_desccheck = '请输入小组描述';
		}
		if ($data_wrong) {
			$main_smarty->assign('namecheck',$namecheck);
			$main_smarty->assign('grp_desccheck',$grp_desccheck);
			$main_smarty->assign('tags', $_POST['tags']);
			$main_smarty->assign('grp_name',$grp_name);
			$main_smarty->assign('grp_privacy',$grp_privacy);
			$main_smarty->assign('grp_desc',$grp_desc);
			$main_smarty->assign('tpl_center', $the_template . '/groupnew');
			$main_smarty->display($the_template . '/pligg.tpl');
		}
		else {
			//create new grp
			
			$new_grp = new Group();
			$new_grp->name = $grp_name;
			$new_grp->privacy = $grp_privacy;
			$new_grp->desc = $grp_desc;
			$new_grp->store();
			
			// add group user as owner
			$new_grp->setowner($current_user->user_id);
			
			//create new link
			$grpid = $new_grp->id;
			$newlink = new Link();
			$newlink->url = "group.php?grp_id=" . $grpid;
			$newlink->randkey = rand(5000,50000000);
			//$newlink->get($newlink->url);
			$newlink->valid = true;
			$newlink->category = get_category_id("group");
			$newlink->status='published';
			$newlink->published_date = time();
			$newlink->author=$current_user->user_id;
			$newlink->title = strip_tags(trim($new_grp->name));
			$newlink->title_url = makeUrlFriendly($newlink->title);
			$newlink->tags = tags_normalize_string(strip_tags(str_replace('，',',',trim($_POST['tags']))));
			$newlink->content = strip_tags($newlink->title, Story_Content_Tags_To_Allow);
			$newlink->content = str_replace("\n", "<br />", $newlink->content);
			$newlink->link_summary = utf8_substr(strip_tags($newlink->content, Story_Content_Tags_To_Allow), 0, StorySummary_ContentTruncate - 1);
			$newlink->link_summary = str_replace("\n", "<br />", $newlink->link_summary);		
			$newlink->store();
			
			totals_adjust_count('published', 1);
			tags_insert_string($newlink->id, $dblang, $newlink->tags);
			//update grp set linkid
			$new_grp->linkid = $newlink->id;
			$new_grp->store();
			
			//header("Location: story.php?id=" . $newlink->id);
			header("Location: groupadmin.php?grp_id=" . $new_grp->id);
		}
	}
}

function editgrpinfo() {
	global $db, $dblang, $current_user, $main_smarty, $the_template;
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$current_user_type = $group->get_user_type($current_user->user_id);
	if ($current_user_type != 'owner') {
		die('只有小组的创建者才可修改');
	}
	$link = new Link();
	$link->id = $group->linkid;
	$link->read();
	$main_smarty->assign('grp_id', $group->id);
	$main_smarty->assign('grp_name', $group->name);
	$main_smarty->assign('grp_desc', $group->desc);
	$main_smarty->assign('grp_tags', $link->tags);
	$main_smarty->assign('grp_privacy',$group->privacy);
	$main_smarty->assign('tpl_center', $the_template . '/group_editgrpinfo');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function sendinvite() {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $top_users_size, $offset;
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	
	$current_user_type = $group->get_user_type($current_user->user_id);
	if (($current_user_type != 'owner') && ($current_user_type != 'admin')) {
		die('只有小组的创建者和管理员才可发送邀请');
	}
	if (isset($_POST['usernames'])) {
		//deal the search
		$main_smarty->assign('usernames',$_POST['usernames']);
		$main_smarty->assign('cur_page',get_current_page());
		if (trim($_POST['usernames']) != '') {
			$offset=(get_current_page()-1)*$top_users_size;
			$sqlquery = "SELECT count(*) FROM " . table_users . " where user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%'";
			$rows = $db->get_var($sqlquery);
			if (!($offset<$rows)) {
				$offset = 0;//in case the user change the keyword to search
				$main_smarty->assign('cur_page',1);
				$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
			}
			if ($rows>0) {
				$sqlquery = "SELECT * FROM " . table_users . " where user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%'";
				$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
				$searchsql = mysql_query($sqlquery);
				$userlist = array();
				
				while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
				foreach($userlist as $key => $val){
					$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
					$userlist[$key]['User_State_Ingrp'] = $group->get_user_state($val['user_id']);
					if ($userlist[$key]['User_State_Ingrp'] == 'approved') {
						$userlist[$key]['User_Type_Ingrp'] = $group->get_user_type($val['user_id']);;
					}
					else {
						$userlist[$key]['User_Type_Ingrp'] = '';
					}
				}
				$main_smarty->assign('userlist', $userlist);
				$main_smarty->assign('rows', $rows);
			}	
			else {
				//no such user in the system
				$noresult = 'true';
				$main_smarty->assign('noresult',$noresult);
				$main_smarty->assign('nouserhint','系统中没有此用户');
			}
		}
		if (trim($_POST['usernames']) == '') {
			$noresult = 'true';
			$main_smarty->assign('noresult',$noresult);	
		} 
	}
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('sender', $current_user->user_id);
	if ($current_user->user_level == 'god') {
		$main_smarty->assign('senderlevel',0);
	}
	if ($current_user->user_level == 'admin') {
		$main_smarty->assign('senderlevel',1);
	}
	if ($current_user->user_level == 'normal') {
		$main_smarty->assign('senderlevel',2);
	}
	$main_smarty->assign('tpl_center', $the_template . '/group_sendinvite');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function dealapply() {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $top_users_size, $offset;
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	
	$current_user_type = $group->get_user_type($current_user->user_id);
	if (($current_user_type != 'owner') && ($current_user_type != 'admin')) {
		die('只有小组的创建者和管理员才可处理申请');
	}
	$nouserhint = '';
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	$sqlquery = "SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=" .$grp_id . " AND gu_user_state='applied'";
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT * FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='applied' AND gu_group_id=" . $grp_id;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		foreach($userlist as $key => $val){
			$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			
		}
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}	
	else {
		$nouserhint = "无未处理申请";
	}
	$main_smarty->assign('nouserhint', $nouserhint);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('tpl_center', $the_template . '/group_dealapply');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function managemembers() {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $top_users_size, $offset;
	
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$pusher = $group->getpusher();
	$main_smarty->assign('pusher',$pusher);
	$current_user_type = $group->get_user_type($current_user->user_id);
	if (($current_user_type != 'owner') && ($current_user_type != 'admin')) {
		die('只有小组的创建者和管理员才可管理成员');
	}
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	//$sqlquery = "SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=" .$grp_id . " AND gu_user_state='approved'";
	$sqlquery = "SELECT COUNT(*) FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=" . $grp_id;
	$sqlquery .= $keystr;
	//die($sqlquery);
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT * FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=" . $grp_id;
		$sqlquery .= $keystr;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		foreach($userlist as $key => $val){
			$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			$userlist[$key]['User_Type_Ingrp'] = $group->get_user_type($val['user_id']);;
		}
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '小组中无此用户';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('tpl_center', $the_template . '/group_managemembers');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function managerecopeople()  {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $top_users_size, $offset;
	
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$pusher = $group->getrecop_indexshow();
	$main_smarty->assign('pusher',$pusher);
	$current_user_type = $group->get_user_type($current_user->user_id);
	if (($current_user_type != 'owner') && ($current_user_type != 'admin')) {
		die('只有小组的创建者和管理员才可管理小组推荐的人');
	}
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	$sqlquery = "SELECT COUNT(" . table_users . ".user_id) FROM " . table_users . ", " . table_link_owner . ", " . table_group_link . ", " . table_links . " WHERE " . table_users . ".user_id=" . table_link_owner . ".owner_id AND " . table_link_owner . ".link_id=" . table_group_link . ".gl_link_id AND " . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("人") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
	$sqlquery .= $keystr;
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT " . table_users . ".*, " . table_links . ".link_id " . " FROM " . table_users . ", " . table_link_owner . ", " . table_group_link . ", " . table_links . " WHERE " . table_users . ".user_id=" . table_link_owner . ".owner_id AND " . table_link_owner . ".link_id=" . table_group_link . ".gl_link_id AND " . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("人") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
		$sqlquery .= $keystr;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		foreach($userlist as $key => $val){
			$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			$userlist[$key]['User_Type_Ingrp'] = $group->get_user_type($val['user_id']);;
		}
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '小组尚未推荐人';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('tpl_center', $the_template . '/group_managerecopeople');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function managerecothing()   {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $top_users_size, $offset;
	
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( link_title LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$pusher = $group->getrecol_indexshow();
	$main_smarty->assign('pusher',$pusher);
	$current_user_type = $group->get_user_type($current_user->user_id);
	if (($current_user_type != 'owner') && ($current_user_type != 'admin')) {
		die('只有小组的创建者和管理员才可管理小组推荐的物');
	}
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	$sqlquery = "SELECT COUNT(" . table_links . ".link_id) FROM " . table_group_link . ", " . table_links . " WHERE "  . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("物") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
	$sqlquery .= $keystr;
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT " . table_links . ".* FROM " . table_group_link . ", " . table_links . " WHERE "  . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("物") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
		$sqlquery .= $keystr;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '小组尚未推荐物';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('tpl_center', $the_template . '/group_managerecothing');
	$main_smarty->display($the_template . '/pligg.tpl');
}

?>